import Sortable from 'sortablejs';

export default {
    data() {
        return {
            isSorting: false,
            sortData: [],
        }
    },
    methods: {
        startSort(container) {
            this.isSorting = true
            let el = this.$el.querySelector(container);
            this.sortable = Sortable.create(el, {
                //handle: '.cell',
                onEnd: evt => { // 监听拖动结束事件
                    var item = this.sortData.splice(evt.oldIndex, 1)[0]
                    this.sortData.splice(evt.newIndex, 0, item)
                    this.onSortEnd && this.onSortEnd(this.sortData)
                }
            })
        },
        stopSort() {
            this.isSorting = false
            if (this.sortable) {
                this.sortable.option('disabled', true)
                if (this.sortable.el) this.sortable.destroy()
            }
        },
    },
    destroyed() {
        this.stopSort()
        this.sortable = null
    }
}
